package com.example.demo.leetcode.level2;

import java.util.ArrayList;
import java.util.List;

public class 大数相乘 {

    public static String multiply(String num1, String num2) {

        if(num1.equals("0") || num2.equals("0")){
            return "0";
        }

        int [] res = new int [num1.length() + num2.length()];
        for(int i = num1.length()-1;i>=0;i--){
            for(int j = num2.length()-1;j>=0;j--){
                int sum = (num1.charAt(i) - '0') * (num2.charAt(j)-'0') + res[i+j+1];
                res[i+j+1] =  sum % 10;
                res[i+j] =  res[i+j] + sum / 10;
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i=0; i < res.length;i++){
            if(i == 0 && res[i] == 0){
                continue;
            }
            sb.append(res[i]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String multiply = multiply("123", "456");
        System.out.println(multiply);
    }

}
